<template>
	<view class="page">
		<view class="text-right" v-if="detailsObj.status==1" @click="tuijiao">保证金退缴</view>
		<view class="text-right" v-else>未缴保证金</view>
		<view class="flex row-between box">
			<view>保证金缴纳</view>
			<view>￥{{detailsObj.bzj!='0.00'?detailsObj.bzj:money}}</view>
		</view>
		<view class="m-t-100 bold" @click="submit" v-if="detailsObj.status!=1">
			<u-button shape="circle" size="large" type="primary" text="去支付"></u-button>
		</view>
		
		<u-popup :show="show" :round="10" mode="center" @close="close">
			<view class="popup">
				<view class="text-center m-b-60 bold font-size-32">保证金退缴</view>
				<view class="flex row-between box">
					<view>保证金缴纳</view>
					<view>￥{{detailsObj.bzj!='0.00'?detailsObj.bzj:money}}</view>
				</view>
				<view class="m-t-50" @click="sure">
					<u-button shape="circle" type="primary" text="确认"></u-button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	const app = getApp();
	export default {
		data() {
			return {
				show:false,
				money:app.globalData.bzj_price,
				detailsObj: {
					bzj:'0.00'
				}
			}
		},
		onLoad(options) {
			console.log(options.items)
			if(options.items){
				this.detailsObj = JSON.parse(decodeURIComponent(options.items))
			}
			console.log(this.money)
		},
		methods: {
			async submit(){
				let res = await this.$api.bzj_pay();
				if (res.code == 1) {
					// #ifdef APP-PLUS
					await this.paymoney(res.data.order_id,res.data.from,6)
					// #endif
					// #ifdef MP-WEIXIN
					await this.paymoney(res.data.order_id,res.data.from,1)
					// #endif
				}
			},
			async paymoney(id,str,type){
				let that = this;
				let res = await this.$api.prepay({
					order_id:id,
					from:str,
					type:type,
					pay_way:2
				});
				if (res.code == 1) {
					if(type==1){
						uni.requestPayment({
							provider: 'wxpay',
							timeStamp: res.data.config.timeStamp, // 时间戳（单位：秒）
							nonceStr: res.data.config.nonceStr, // 随机字符串
							package: res.data.config.package, // 固定值
							signType: res.data.config.signType, // 签名算法，这里用的 MD5/RSA 签名
							paySign: res.data.config.paySign, //签名
							success(res) {
								that.$toast({
									title: '支付成功！',
									icon: 1
								});
								app.globalData.is_vip = 1;
								setTimeout(function () {
									uni.navigateBack();
								}, 1000);
							},
							fail(e) {
								that.$toast({
									title: '支付失败！'
								});
							}
						});
					}else if(type==6){
						uni.requestPayment({
						    provider: 'alipay',
						    orderInfo: 'orderInfo', //微信、支付宝订单数据 【注意微信的订单信息，键值应该全部是小写，不能采用驼峰命名】
						    success: function (res) {
						        console.log('success:' + JSON.stringify(res));
								this.$toast({
									title: '支付成功！',
									icon: 1
								});
								setTimeout(function () {
									uni.navigateBack();
								}, 1000);
						    },
						    fail: function (err) {
						        console.log('fail:' + JSON.stringify(err));
								this.$toast({
									title: '支付失败！'
								});
						    }
						});
					}
				}
			},
			async sure(){
				let res = await this.$api.tuiBzj();
				if (res.code == 1) {
					this.$toast({
						title: '已申请，请耐心等待！',
						icon: 1
					});
					setTimeout(function () {
						uni.navigateBack();
					}, 1000);
				}
			},
			tuijiao(){
				this.show = true;
			},
			close() {
				this.show = false;
			}
		}
	}
</script>

<style lang="scss" scoped>
.page{
	padding: 30rpx 35rpx;
	font-size: 30rpx;
	
	.text-right{
		color: #457FD3;
		font-weight: 400;
		margin-bottom: 70rpx;
	}
	
	.box{
		border: 1px solid #eee;
		border-radius: 25rpx;
		padding: 30rpx 25rpx;
		
		view:last-child{
			color: #457FD3;
		}
	}
	
	.popup{
		width: 575rpx;
		height: 478rpx;
		padding: 40rpx 37rpx;
		box-sizing: border-box;
	}
}
</style>
